﻿/*
 * Created by SharpDevelop.
 * User: Dhanesh
 * Date: 11-10-2008
 * Time: 04:00:17
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace ChallanSystem
{
	/// <summary>
	/// Description of Transporter.
	/// </summary>
	public class Transporter
	{
		public Transporter()
		{
		}
		private static string strCon = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=./Data/DB.mdb" ;
		private OleDbConnection myCon = new OleDbConnection(strCon);
		
		private string name = String.Empty;
		
		public string Name {
			get { return name; }
			set { name = value; }
		}
		private string address1 = String.Empty;
		
		public string Address1 {
			get { return address1; }
			set { address1 = value; }
		}
		private string address2 = String.Empty;
		
		public string Address2 {
			get { return address2; }
			set { address2 = value; }
		}
		private string address3 = String.Empty;
		
		public string Address3 {
			get { return address3; }
			set { address3 = value; }
		}
		private string email = String.Empty;
		
		public string Email {
			get { return email; }
			set { email = value; }
		}
		private string postcode;
		
		public string Postcode {
			get { return postcode; }
			set { postcode = value; }
		}
		private string phone1;
		
		public string Phone1 {
			get { return phone1; }
			set { phone1 = value; }
		}
		private string phone2;
		
		public string Phone2 {
			get { return phone2; }
			set { phone2 = value; }
		}
		private string fax;
		
		public string Fax {
			get { return fax; }
			set { fax = value; }
		}
				
		#region View()
		public DataSet View()
		{
			string sqlStr = "SELECT * FROM Transporters";
			//Make a DataSet object
			DataSet myDataSet = new DataSet();
			//Using the OleDbDataAdapter execute the query
			OleDbDataAdapter myAdapter = new OleDbDataAdapter( sqlStr , myCon );
			//Fill the DataSet with the Table 'Product'
			myAdapter.Fill(myDataSet,"Transporters");
			return myDataSet;
		}
		#endregion
		
		#region Update()
		public bool Update(int id)
		{
			bool done = false;
			
			if( name != "" && address1 != "" && phone1 != null)
			{
				//the string to get values from the textboxes and form an "INSERT INTO"
				// statement.
				string sqlStr = "Update Transporters SET ";
				sqlStr += "Transporter_Name = '" + Convert.ToString(name) + "',";
				sqlStr += "Address1 = '" + Convert.ToString(address1) + "',";
				sqlStr += "Address2 = '" + Convert.ToString(address2) + "',";
				sqlStr += "Address3 = '" + Convert.ToString(address3) + "',";
				sqlStr += "Postcode = '" + Convert.ToString(postcode) + "',";
				sqlStr += "Fax = '" + Convert.ToString(fax) + "',";
				sqlStr += "Phone1 = '" + Convert.ToString(phone1) + "',";
				sqlStr += "Phone2 = '" + Convert.ToString(phone2) + "',";
				sqlStr += "Email = '" + Convert.ToString(email) + "' ";
				sqlStr += "WHERE  Transporter_ID= " + Convert.ToString(id);
				
				OleDbCommand myCmd = new OleDbCommand( sqlStr , myCon ) ;
				try
				{
					myCon.Open();
					//Execute the statement
					myCmd.ExecuteNonQuery();
					//statusBar.Text="Connected - Now you can add records";
					done = true;
					
				}
				catch( OleDbException a)
				{
					MessageBox.Show("Error in updating! \n" + a.ToString(), "Error");
				}
				finally
				{
					myCmd.Dispose();
					myCon.Close() ;
					//myCon.Dispose();
				}
			}
			else
			{
				MessageBox.Show("All mandatory fields must be filled.", "Error");
			}
			return done;
		}
		#endregion
		
		#region Add()
		public bool Add()
		{
			
			bool done = false;
			
			if( name != "" && address1 != "" && phone1 != null)
			{
				//the string to get values from the textboxes and form an "INSERT INTO"
				// statement.
				string sqlStr = "INSERT INTO Transporters(Transporter_Name,Address1,Address2,Address3,Postcode,Fax,Phone1,Phone2,Email) Values(";
				sqlStr += "'" + Convert.ToString(name) + "',";
				sqlStr += "'" + Convert.ToString(address1) + "',";
				sqlStr += "'" + Convert.ToString(address2) + "',";
				sqlStr += "'" + Convert.ToString(address3) + "',";
				sqlStr += "'" + Convert.ToString(postcode) + "',";
				sqlStr += "'" + Convert.ToString(fax) + "',";
				sqlStr += "'" + Convert.ToString(phone1) + "',";
				sqlStr += "'" + Convert.ToString(phone2) + "',";
				sqlStr += "'" + Convert.ToString(email) + "')";

				OleDbCommand myCmd = new OleDbCommand( sqlStr , myCon ) ;
				try
				{
					myCon.Open();
					//Execute the statement
					myCmd.ExecuteNonQuery();
					//statusBar.Text="Connected - Now you can add records";
					done = true;
					
				}
				catch( OleDbException a)
				{
					MessageBox.Show("Error in inserting! \n" + a.ToString(), "Error");
				}
				finally
				{
					myCmd.Dispose();
					myCon.Close() ;
					//myCon.Dispose();
				}
			}
			else
			{
				MessageBox.Show("All fields must be completed.", "Error");
			}
			return done;
		}
		#endregion
		
		#region Delete()
		public bool Delete(int id)
		{
			bool done = false;
					
			//the string to get values from the textboxes and form an "INSERT INTO"
			// statement.
			string sqlStr = "DELETE FROM Transporters ";
			sqlStr += "WHERE Transporter_ID= " + Convert.ToString(id);
			
			OleDbCommand myCmd = new OleDbCommand( sqlStr , myCon ) ;
			try
			{
				myCon.Open();
				//Execute the statement
				myCmd.ExecuteNonQuery();
				//statusBar.Text="Connected - Now you can add records";
				done = true;
				
			}
			catch( OleDbException a)
			{
				MessageBox.Show("Error in deleting! \n" + a.ToString(), "Error");
			}
			finally
			{
				myCmd.Dispose();
				myCon.Close() ;
				//myCon.Dispose();
			}
			
			return done;
		}
		#endregion
	}
}
